package com.cooper.service.account;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.constants.ResourceConstants;
import com.cooper.dao.account.SendAddressDAO;
import com.cooper.entity.LocationEntity;
import com.cooper.entity.SendAddressEntity;
import com.cooper.entity.UserInfo;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.service.admin.LocationService;
import com.cooper.util.BeanUtils;
import com.cooper.util.Controllers;
import com.cooper.vo.AddressSearchVo;
import com.cooper.vo.SendAddressVo;

@Service
public class SendAddressService {
	
	@Autowired
	private LocationService locationService;
	
	@Autowired
	private SendAddressDAO sendAddressDao;
	
	public Long saveAddressVo(SendAddressVo addressVo, UserInfo userInfo, String origin, String lang) throws Exception{
		SendAddressEntity entity = new SendAddressEntity();
		BeanUtils.copyProperties(addressVo, entity);
		entity.setOrigin(origin);
		entity.setOuterId(userInfo.getId());
		Controllers.prepareEntityForDataSaving(entity, userInfo, false);
		return this.saveEntity(entity, lang);
	}
	
	public Long saveEntity(SendAddressEntity entity, String lang) throws Exception{
		long countryId = entity.getCountryId();
		LocationEntity parentEntity = this.locationService.loadById(countryId);
		entity.setContinentId(parentEntity.getId());
		entity.setContinentName(ResourceConstants.Lang.ZH_CN.equals(lang)?parentEntity.getZhCn():parentEntity.getEnUs());
		return this.sendAddressDao.saveEntity(entity);
	}
	
	public SendAddressEntity loadById(long addressId) throws Exception{
		return this.sendAddressDao.loadById(addressId);
	}
	
	@SuppressWarnings("unchecked")
	public PageResult<SendAddressEntity> queryForList(AddressSearchVo searchVo, PageInfo pageInfo) throws Exception {
		int rowCount = this.sendAddressDao.queryAddressCount(searchVo);
		pageInfo.setRowCount(rowCount);
		if(0 == rowCount){
			return new PageResult<SendAddressEntity>(Collections.EMPTY_LIST, pageInfo);
		}
		List<SendAddressEntity> dataList = this.sendAddressDao.queryAddressList(searchVo, pageInfo);
		return new PageResult<SendAddressEntity>(dataList, pageInfo);
	}
	
	public List<SendAddressEntity> findByOriginAndOuterId(String origin, long outerId) throws Exception{
		return this.sendAddressDao.findByOriginAndOuterId(origin, outerId);
	}

	public boolean updateEntity(SendAddressEntity entity) throws Exception{
		return this.sendAddressDao.updateEntity(entity);
	}

	public boolean deleteEntity(SendAddressEntity entity) throws Exception{
		return this.sendAddressDao.deleteEntity(entity);
	}

	public void setAsDefault(String origin, Long userId, Long addressId, boolean result) throws Exception{
		if(result){
			this.sendAddressDao.releaseDefaultOther(origin, userId, addressId);
			this.sendAddressDao.setAsDefault(addressId);
		} else {
			this.sendAddressDao.releaseDefaultThis(origin, userId, addressId);
		}
	}

}
